package com.dgj.sigar.gathers;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.hyperic.sigar.NetInterfaceConfig;
import org.hyperic.sigar.NetInterfaceStat;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;

/**
 * 网卡信息收集器
 * 
 * @author wkipy
 *
 */
public class NetInterfaceGather {

	private String ip;

	private String hostName;

	private NetInterface[] netInterfaces;

	public String getIp() {
		return ip;
	}

	public String getHostName() {
		return hostName;
	}

	public NetInterface[] getNetInterfaces() {
		return netInterfaces;
	}

	{
		try {
			ip = InetAddress.getLocalHost().getHostAddress();
			hostName = InetAddress.getLocalHost().getHostName();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}

	public static NetInterfaceGather init(Sigar sigar) {
		NetInterfaceGather gather = new NetInterfaceGather();
		try {
			String[] nis = sigar.getNetInterfaceList();
			gather.netInterfaces = new NetInterface[nis.length];
			for (int i = 0; i < nis.length; i++) {
				gather.netInterfaces[i] = new NetInterface(nis[i], sigar);
			}
		} catch (SigarException e) {
			e.printStackTrace();
		}
		return gather;
	}

	/**
	 * 
	 * @author wkipy
	 *
	 */
	public static class NetInterface {
		private String name;
		private NetInterfaceConfig config;

		private NetInterfaceStat stat;

		public String getName() {
			return name;
		}

		public NetInterfaceConfig getConfig() {
			return config;
		}
		
		

		public NetInterfaceStat getStat() {
			return stat;
		}

		public NetInterface(String name, Sigar sigar) {
			this.name = name;
			try {
				this.config = sigar.getNetInterfaceConfig(name);
				this.stat = sigar.getNetInterfaceStat(name);
			} catch (SigarException e) {
				e.printStackTrace();
			}
		}
	}

}
